package com.example.hello.common;

import com.fasterxml.jackson.annotation.JsonInclude;

/**
 * 统一响应结果类
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Result<T> {
    
    private Integer code;
    private String msg;
    private T data;
    
    // 构造函数
    public Result() {}
    
    public Result(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }
    
    // 成功响应
    public static <T> Result<T> success() {
        return new Result<>(1, "success", null);
    }
    
    public static <T> Result<T> success(T data) {
        return new Result<>(1, "success", data);
    }
    
    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(1, msg, data);
    }
    
    // 失败响应
    public static <T> Result<T> error() {
        return new Result<>(0, "error", null);
    }
    
    public static <T> Result<T> error(String msg) {
        return new Result<>(0, msg, null);
    }
    
    public static <T> Result<T> error(Integer code, String msg) {
        return new Result<>(code, msg, null);
    }
    
    // Getter和Setter方法
    public Integer getCode() {
        return code;
    }
    
    public void setCode(Integer code) {
        this.code = code;
    }
    
    public String getMsg() {
        return msg;
    }
    
    public void setMsg(String msg) {
        this.msg = msg;
    }
    
    public T getData() {
        return data;
    }
    
    public void setData(T data) {
        this.data = data;
    }
    
    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}